home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13660 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  63 lines

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help, I think with an array
  5. Date: 9 Apr 1996 10:03:44 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ke59gINNkpi@mayne.ugrad.cs.ubc.ca>
  8. References: <4kbhcl$p4l@uwm.edu> <3169B805.8BE@willows.com>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <3169B805.8BE@willows.com>,
  12. Tarang Deshpande  <tarang@willows.com> wrote:
  13. >Mario David Uy wrote:
  14. >> 
  15. >> I am a beginner to programming and I have a problem.
  16. >> Well, I have this one table that I want to use.  I looks somewhat like this.
  17. >> 
  18. >> Age           points
  19. >> 30             -3
  20. >> 31             -2
  21. >> 32-33          -1
  22. >> 34              0
  23. >> 35-36           1
  24.  
  25. [ snip ]
  26.  
  27.  
  28. >Yes you should be using an array to store the table data but no if...else.  
  29. >Instead of if...else you should use a loop through the array.  If you tell me more
  30. >information like are the tables of the same size, are the age values in all the
  31. >tables the same, etc.  Then I can post a more detailed answer.
  32.  
  33. Why should he loop through the array? I think he wants a direct lookup:
  34.  
  35. #include <stdio.h>
  36.  
  37. static int points[] = {
  38.      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,        /*  0 ...  9    */
  39.      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,        /* 10 ... 19    */
  40.      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,        /* 20 ... 29    */
  41.     -3, -2, -1, -1,  0,  1,  1,  0,  0,  0,        /* 30 ... 39    */
  42. };
  43.  
  44. #define MAXAGE    (sizeof(points)/sizeof(points[0])-1)
  45.  
  46. int main()
  47.  
  48. {
  49.     unsigned int age;
  50.  
  51.     do {
  52.         printf("Enter age:\n");
  53.         scanf("%u",&age);
  54.     } while (age > MAXAGE);
  55.  
  56.     printf("points associated with age %d = %d\n",age,points[age]);
  57.  
  58.     return 0;
  59. }
  60.  
  61. -- 
  62.  
  63.